package com.nanbei.eduService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nanbei.eduService.config.SubjectExcelListener;
import com.nanbei.eduService.entity.EduSubject;
import com.nanbei.eduService.entity.excel.SubjectData;
import com.nanbei.eduService.entity.vo.OneEduSubject;
import com.nanbei.eduService.entity.vo.TwoEduSubject;
import com.nanbei.eduService.mapper.EduSubjectMapper;
import com.nanbei.eduService.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-28
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * EasyExcel上传文件数据
     * @param file   上传的文件
     * @param eduSubjectService   监听器无法交有Spring管理   需要自己定义
     */
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {

        //文件的输入流
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     *  课程按照一级和二级目录进行查看分类
     * @return
     */
    @Override
    public List<OneEduSubject> findOneTwoSubject() {

        //查看一级目录的课表
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id" ,"0");
        List<EduSubject> onrEduSubjects = baseMapper.selectList(queryWrapper);

        //查看二级目录的课表

        QueryWrapper<EduSubject> queryWrapper1  = new QueryWrapper<>();
        queryWrapper1.ne("parent_id" ,"0");
        List<EduSubject> twoEduSubjects = baseMapper.selectList(queryWrapper1);

        //创建一个集合  用于存储最终封装的数据
        List<OneEduSubject> finalSubject = new ArrayList<>();

        //封装一级分类
        //查询出来所有的一级分类list集合遍历，得到每个一级分类的对象，获取每一个一级分类的对象值
        //封装到最终的集合中List<OneEduSubject> finalSubject
        for (EduSubject onrEduSubject : onrEduSubjects) {

            OneEduSubject oneEduSubject = new OneEduSubject();

            //将一级目录的值复制到 自定义的一级目录数组里面
            BeanUtils.copyProperties(onrEduSubject,oneEduSubject);

            //定义一个数组存放二级目录
            List<TwoEduSubject> twoEduSubjectsFinal = new ArrayList<>();

            for (EduSubject twoEduSubject : twoEduSubjects) {

                //判断二级目录的ParentId 是不是 当前一级目录的ID
                if (twoEduSubject.getParentId().equals(onrEduSubject.getId())) {

                    TwoEduSubject twoEduSubject1 = new TwoEduSubject();

                    //将二级目录的值复制到 自定义的二级目录数组里面
                    BeanUtils.copyProperties(twoEduSubject,twoEduSubject1);
                    //将改对象存储到定义的二级目录数组里面
                    twoEduSubjectsFinal.add(twoEduSubject1);
                }
            }

            //将二级目录遍历出来的数组存放到一级目录里面
            oneEduSubject.setChildren(twoEduSubjectsFinal);
            finalSubject.add(oneEduSubject);
        }

        return finalSubject;
    }
}
